package ControllerLayer.ControlServices.Preprocessor;

import DataLayer.ImageDataContainer.ImageInfo.IImageInfo;
import java.awt.color.ColorSpace;
import java.awt.image.ByteLookupTable;
import java.awt.image.ColorConvertOp;
import java.awt.image.LookupOp;

public class MainPreprocessor implements IPreprocessor {

    private int threshold = 160;

    /**
     * Preprocesses the original loaded image.
     * @param imgInfo - Contains all the information about the loaded image.
     */
    public void preprocessOriginalImage(IImageInfo imgInfo) {

        ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);

        imgInfo.setPreprocessedImage(colorConvert.filter(imgInfo.getOriginalImage(), null));

        byte[] thresholdTable = new byte[256];
        for (int i = 0; i < 256; i++) {
            thresholdTable[i] = (byte) ((i < threshold) ? 0 : -1);
            ////System.out.print(thresholdTable[i] + " s ");
        }

        LookupOp op = new LookupOp(new ByteLookupTable(0, thresholdTable), null);

        imgInfo.setBWImage(op.filter(imgInfo.getPreprocessedImage(), null));

        //MATLAB PREPROCESSOR - DOESN'T WORK - ISSUE - WHY??
        //ExternalPreprocessor extPrep = new ExternalPreprocessor("alma");
        //extPrep.preprocessImage();
    }
}
